package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class forgotPassPopupPanel extends DialogBox implements Serializable {

	private static final long serialVersionUID = 4468006641815672683L;
	private DatabaseHandlerServiceAsync databaseHandlerService = (DatabaseHandlerServiceAsync) GWT.create(DatabaseHandlerService.class);	

	AbsolutePanel absolutePanel;
	TextBox userID;
//	PasswordTextBox pass, retypePass;
	CheckBox agree;
	Button change, cancel;
	
	Vector<Volunteer> volunteerVector;
	
	public forgotPassPopupPanel() {
		this.setText("Forgot Password");
		
		absolutePanel = new AbsolutePanel();
		setWidget(absolutePanel);
		absolutePanel.setSize("276px", "228px");
		
		userID = new TextBox();
//		pass = new PasswordTextBox();
//		retypePass = new PasswordTextBox();
		agree = new CheckBox("I agree to reset my password");
		change = new Button("Change");
		cancel = new Button("Cancel");
		
		absolutePanel.add(new Label("Email Address:"), 10, 78);
		absolutePanel.add(userID, 10, 96);
		userID.setSize("192px", "13px");
		
//		absolutePanel.add(new Label("New Password"));
//		absolutePanel.add(pass);
//		pass.setWidth(COL_WIDTH);
//		
//		absolutePanel.add(new Label("Retype New Password"));
//		absolutePanel.add(retypePass);
//		retypePass.setWidth(COL_WIDTH);
		absolutePanel.add(agree, 10, 140);
		
		absolutePanel.add(change, 52, 194);
		change.setSize("85px", "24px");
		absolutePanel.add(cancel, 143, 194);
		cancel.setSize("67px", "24px");
		
		change.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				try {
					if (agree.getValue()) {
						databaseHandlerService.forgotPassword(userID.getValue(), new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								Window.alert("Failed to generate random new password.");
							}

							@Override
							public void onSuccess(Boolean result) {
								// TODO Auto-generated method stub
								if(result) {
									if (Window.confirm("Success! Check your email for the new Password"))
										hide();
								} else {
									Window.alert("Failed to generate random new password.");
								}
							}
							
						});
					} else
						Window.alert("You must agree to reset your password.");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		  });
		
		cancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				hide();
			}
		  });
		
		databaseHandlerService.getAllVolunteers(new AsyncCallback<Vector<Volunteer>>() {
			public void onFailure(Throwable caught) {
				Window.alert("Failed to get volunteers list.");
			}
	
			public void onSuccess(Vector<Volunteer> result) {
				try {
					volunteerVector = result;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		userID.setFocus(true);
		
		Label lblThisWillReset = new Label("This will reset your password and send a new one to the email address you specify below.");
		absolutePanel.add(lblThisWillReset, 10, 10);
		lblThisWillReset.setSize("256px", "54px");
	}
	
	Volunteer vol = null;
	private ClosingHandler handler;
	
	public void changePass(String pass) {
		int i;
		for (i=0; i < volunteerVector.size(); i++) {
			if (userID.getValue() == volunteerVector.get(i).getUsername()) {
				volunteerVector.get(i).setPassword(pass);
				vol = new Volunteer(volunteerVector.get(i).getFirstName(), volunteerVector.get(i).getLastName(),
						volunteerVector.get(i).getEmail(), volunteerVector.get(i).getCarrier(), volunteerVector.get(i).getCellPhoneNumber(),
						volunteerVector.get(i).getSmsEmail(), volunteerVector.get(i).getUsername(), 
						volunteerVector.get(i).getPassword(), volunteerVector.get(i).getFreeTimeEvents());
			}
			if (i == volunteerVector.size()) {
				Window.alert("User ID is not found. Please create new user ID");
				Window.addWindowClosingHandler(handler);
			}
				
		}
		
		databaseHandlerService.write(vol, new AsyncCallback<Integer>() {
			
			public void onFailure(Throwable caught) {
				Window.alert("Failed to update info");
			}

			public void onSuccess(Integer result) {
				try {
					Window.confirm("Success!!!!!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
